package cn.kgc.vue.controller;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Role;
import cn.kgc.vue.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/7/4
 * @description
 */
@RestController
@RequestMapping("role")
@Api(tags = "角色模块")
public class RoleController {

    @Resource
    private RoleService roleService;

    @ApiOperation("角色列表")
    @GetMapping("list")
    public ResponseResult roleList(){

        List<Role> list = roleService.list();
        return  ResponseResult.success().data("roleList",list);
    }


    @ApiOperation("角色权限列表")
    @GetMapping("rolePermissions/{rid}")
    public ResponseResult rolePermissions(@PathVariable("rid") Integer rid){

        return  roleService.rolePermissions(rid);
    }

    @RequiresPermissions("sys:role:select")
    @ApiOperation("角色分页")
    @PostMapping ("page")
    public ResponseResult rolePage(@RequestBody QueryCondition<Role> params){

        return roleService.rolePage(params);
    }

    @RequiresPermissions("sys:role:add")
    @ApiOperation("添加角色")
    @PostMapping ("add")
    public ResponseResult addRole(@RequestBody Role role){
        roleService.save(role);
        return ResponseResult.success().message("角色添加成功");
    }


    @RequiresPermissions("sys:role:edit")
    @ApiOperation("编辑角色")
    @PutMapping("edit")
    public ResponseResult edit(@RequestBody Role role){
        roleService.updateById(role);
        return ResponseResult.success().message("角色更新成功");
    }


    @RequiresPermissions("sys:role:delete")
    @ApiOperation("删除角色")
    @DeleteMapping("delete/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id){

        return roleService.delete(id);
    }


    // editRolePermissions
    @ApiOperation("分配角色权限")
    @PutMapping("editRolePermissions")
    public ResponseResult editRolePermissions(@RequestBody HashMap<String,Object> params){

        return roleService.editRolePermissions(params);
    }
}
